package com.chain.BrotherExchange.utils;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

public class AppLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {

    public interface AppDestroyedCallback {
        /**
         * App内所以Activity被销毁时回调
         */
        void onAppDestroyed();
    }

    private int mActivityStackNumber;
    private AppDestroyedCallback mCallback;

    public AppLifecycleCallbacks(AppDestroyedCallback callback) {
        this.mCallback = callback;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        mActivityStackNumber += 1;
    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        mActivityStackNumber -= 1;
        if (mActivityStackNumber == 0) {
            mCallback.onAppDestroyed();
        }
    }
}
